/**
 * OpenAction.java
 * @author: liuex
 * 2007-12-28 ����10:15:16
 *
 * note: 
 */
package org.lex.explorer.file;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.KeyStroke;

public class RenameAction extends AbstractFileAction {
	public RenameAction(FileExplorer fileExplorer) {
		super(fileExplorer);
		this.setName("Rename");
		this.setAcceleratorKey(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
	}

	protected void actionnPerformed(ActionEvent e) {
		File file = fileExplorer.getSelectedFile();
		if (null == file)
			return;
		String name = this.input("Rename", "Enter new name:", file.getName());
		if (null == name)
			return;
		File to = new File(fileExplorer.getDirectory(), name);
		if (to.exists()) {
			this.error("Rename error", "File exists: " + name);
			return;
		} else {
			try {
				file.renameTo(to);
				fileExplorer.refresh();
			} catch (Exception ex) {
				this.error("Rename error", ex.getLocalizedMessage());
			}
		}
	}
}
